package org.csu.myblogger.myblogger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.Getter;
import org.csu.myblogger.myblogger.commom.CommonResponce;
import org.csu.myblogger.myblogger.entity.Account;
import org.csu.myblogger.myblogger.entity.Article;
import org.csu.myblogger.myblogger.entity.ArticleKinds;
import org.csu.myblogger.myblogger.entity.ArticleStart;
import org.csu.myblogger.myblogger.mapper.*;
import org.csu.myblogger.myblogger.service.service.ArticleService;
import org.csu.myblogger.myblogger.vo.ArticleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Getter
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ArticleKindsMapper articleKindsMapper;
    @Autowired
    private ArticleStartMapper articleStartMapper;
    @Override
    public CommonResponce PublishArticle(String username, ArticleVo articleVo) {
        Article article=new Article();
        BeanUtils.copyProperties(articleVo,article);

        if (articleVo.getArticle_author()!=username){
            return CommonResponce.createForError(1,"用户名不一致，请检查");
        }
        try{
            articleMapper.insert(article);
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"文章发布失败");
        }
        return CommonResponce.CreatForSuccess("发表成功");
    }

    @Override
    public CommonResponce EditArticle(String username,String article_author, ArticleVo articleVo) {
        Article article=new Article();
        BeanUtils.copyProperties(articleVo,article);

        if (articleVo.getArticle_author()!=username){
            return CommonResponce.createForError(1,"用户名不一致，请检查");
        }

        LambdaUpdateWrapper<Article>articleLambdaUpdateWrapper=new LambdaUpdateWrapper<Article>
                ()
                .eq(Article::getArticle_author,username)
                .eq(Article::getArticle_title,article_author)
                .set(Article::getArticle_title,article.getArticle_author())
                .set(Article::getArticle_content,article.getArticle_content())
                .set(Article::getArticle_class,article.getArticle_class())
                .set(Article::getArticle_img,article.getArticle_img());
        try{
            articleMapper.update(articleLambdaUpdateWrapper);
            return CommonResponce.CreatForSuccess("文章修改成功");
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"编辑失败");
        }
    }

    @Override
    public CommonResponce DeleteArticle(String username, ArticleVo articleVo) {
        Article article=new Article();
        BeanUtils.copyProperties(articleVo,article);

        if (articleVo.getArticle_author()!=username){
            return CommonResponce.createForError(1,"用户名不一致，请检查");
        }

        try{
            QueryWrapper<Article>articleQueryWrapper=new QueryWrapper<Article>();
            articleMapper.delete(articleQueryWrapper);
            return CommonResponce.CreatForSuccess("文章删除成功");
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"编辑失败");
        }
    }

    //文章分类
    @Override
    public CommonResponce ArticleClassification(String username, ArticleVo articleVo) {
        ArticleKinds articleKinds=new ArticleKinds();
        articleKinds.setArticleid(articleVo.getArticle_id());
        switch (articleVo.getArticle_class()){
            case"technology":
                articleKinds.setTechnology(true);
                break;
            case "live":
                articleKinds.setLive(true);
                break;
            case "study":
                articleKinds.setStudy(true);
                break;
            case "game":
                articleKinds.setGame(true);
                break;
            case "movies":
                articleKinds.setMovies(true);
                break;
        }
        try {
            articleKindsMapper.insert(articleKinds);
            return CommonResponce.CreatForSuccess("分类成功");
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"分类失败");
        }
    }

    //收藏文章
    @Override
    public CommonResponce ArticleBookmark(String username, ArticleVo articleVo) {
        Article article=new Article();
        BeanUtils.copyProperties(article,article);
        //先判断是否收藏了
        QueryWrapper<ArticleStart>articleStartQueryWrapper=new QueryWrapper<>();
        articleStartQueryWrapper.eq("username",username)
                .eq("articleid",article.getArticle_id());
        if (articleStartMapper.selectOne(articleStartQueryWrapper)!=null){
            return CommonResponce.createForError(1,"收藏失败，已经收藏了");
        }
        //先找到这篇文章，并更新收藏量
        LambdaUpdateWrapper<Article>articleLambdaUpdateWrapper=new LambdaUpdateWrapper<Article>
                ()
                .eq(Article::getArticle_id,article.getArticle_id())
                .eq(Article::getArticle_title,article.getArticle_title())
                .set(Article::getArticle_stare,article.getArticle_stare()+1);
        articleMapper.update(articleLambdaUpdateWrapper);
        //之后建立关系
        try{
            ArticleStart articleStart=new ArticleStart();
            articleStart.setArticleid(article.getArticle_id());
            articleStart.setUsername(username);
            articleStartMapper.insert(articleStart);
            return CommonResponce.CreatForSuccess("收藏成功");
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"收藏失败");
        }
    }

    //取消收藏
    @Override
    public CommonResponce CancleArticleBookmark(String username, ArticleVo articleVo) {
        Article article=new Article();
        BeanUtils.copyProperties(article,article);
        //先判断是否取消收藏了
        QueryWrapper<ArticleStart>articleStartQueryWrapper=new QueryWrapper<>();
        articleStartQueryWrapper.eq("username",username)
                .eq("articleid",article.getArticle_id());
        if (articleStartMapper.selectOne(articleStartQueryWrapper)==null){
            return CommonResponce.createForError(1,"取消收藏失败，已经取消收藏了");
        }
        //先找到这篇文章，并更新收藏量
        LambdaUpdateWrapper<Article>articleLambdaUpdateWrapper=new LambdaUpdateWrapper<Article>
                ()
                .eq(Article::getArticle_id,article.getArticle_id())
                .eq(Article::getArticle_title,article.getArticle_title())
                .set(Article::getArticle_stare,article.getArticle_stare()-1);
        articleMapper.update(articleLambdaUpdateWrapper);
        //之后建立关系
        try{
            ArticleStart articleStart=new ArticleStart();
            articleStart.setArticleid(article.getArticle_id());
            articleStart.setUsername(username);
            articleStartMapper.deleteById(articleStart);
            return CommonResponce.CreatForSuccess("取消收藏成功");
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"取消收藏失败");
        }
    }

    @Override
    public CommonResponce SearchArticle(String article_title) {
        //分情况搜索（暂时放弃）
        try {
            QueryWrapper<Article>articleQueryWrapper=new QueryWrapper<>();
            articleQueryWrapper.eq("article_title",article_title);
            List<Article>articleList=articleMapper.selectList(articleQueryWrapper);
            if (articleList.isEmpty()){
                return CommonResponce.CreatForSuccess(0,"没有查到该文章",null);
            }else return CommonResponce.CreatForSuccess("查询成功",articleList);
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"查询失败");
        }
    }
}
